<?php
/**
 * created by zzy
 * date: 2017/10/20 18:00
 */

namespace App\Eloquent\Ygt;

class Privilege extends DbEloquent{

    protected $table    = 'ygt_privilege';
    // 取对应的权限列表
    public static function getPermissionList( $privilegeId ){

        $join               =[
            [ 'table'=>'ygt_privilege_permission as pp', 'field_l'=>'pp.privilege_id', 'field_c'=>'=', 'field_r'=>'ygt_privilege.id' ],
            [ 'table'=>'ygt_permission as p', 'field_l'=>'pp.permission_id', 'field_c'=>'=', 'field_r'=>'p.id' ]
        ];
        $where              = [ 'ygt_privilege.id'=>$privilegeId ];
        $field              = 'p.id,p.type,p.type_id,p.title';
        $result             = self::getList( $where, $field, '', '', ['p.id','desc'], '', $join );
        return $result;
    }
    // 取对应的工作按钮列表
    public static function getAppindexList( $privilegeId ){

        $join               =[
            [ 'table'=>'ygt_privilege_appindex as a', 'field_l'=>'a.privilege_id', 'field_c'=>'=', 'field_r'=>'ygt_privilege.id' ],
            [ 'table'=>'ygt_appindex as b', 'field_l'=>'a.appindex_id', 'field_c'=>'=', 'field_r'=>'b.id' ]
        ];
        $where              = [
            'ygt_privilege.id'=>$privilegeId,
            'b.id'=>['>',0],
            'b.deleted_at'=>['null',null],
        ];
        $field              = 'b.id,b.is_show_help,b.help_href,b.type,b.title,b.img_url,b.img_url_active';
        $result             = self::getList( $where, $field, '', '', ['b.id','asc'], '', $join );
        return $result;
    }
    // 取对应的操作节点列表
    public static function getAppnodeList( $privilegeId ){

        $join               =[
            [ 'table'=>'ygt_privilege_appnode as a', 'field_l'=>'a.privilege_id', 'field_c'=>'=', 'field_r'=>'ygt_privilege.id' ],
            [ 'table'=>'ygt_appnode as b', 'field_l'=>'a.appnode_id', 'field_c'=>'=', 'field_r'=>'b.id' ]
        ];
        $where              = [ 'ygt_privilege.id'=>$privilegeId ];
        $field              = 'b.id,b.title,b.alias';
        $result             = self::getList( $where, $field, '', '', ['b.id','asc'], '', $join );
        return $result;
    }
    // 取对应的部门列表列表
    public static function getDepartmentList( $privilegeId ){

        $join               =[
            [ 'table'=>'ygt_privilege_department as a', 'field_l'=>'a.privilege_id', 'field_c'=>'=', 'field_r'=>'ygt_privilege.id' ],
            [ 'table'=>'ygt_department as b', 'field_l'=>'a.department_id', 'field_c'=>'=', 'field_r'=>'b.id' ]
        ];
        $where              = [ 'ygt_privilege.id'=>$privilegeId ];
        $field              = 'b.id,b.title';
        $result             = self::getList( $where, $field, '', '', ['b.id','asc'], '', $join );
        return $result;
    }
    public static function getInfoById( $id ){

        $where              = [ 'id'=>$id ];
        $result             = self::getInfo( $where );
        return $result;
    }

    //该角色是否具有某个操作功能
    public static function getAppnodeId( $privilegeId, $appnodeId )
    {
        $where              = [ 'privilege_id'=>$privilegeId, 'appnode_id'=>$appnodeId ];
        $info               = PrivilegeAppnode::getInfo( $where );
        $result             = false;
        if( $info )
        {
            $result         = true;
        }
        return $result;
    }
    //取某个厂下具有某个功能的所有角色id 7
    public static function getPrivilegeIds( $companyId, $appnodeId )
    {
        $where              = ['company_id'=>$companyId];
        $privilegeList     = Privilege::getList( $where,'id,title' )->toArray();
        $result             = [];
        if( $privilegeList )
        {
            $privilegeIdArr       = [];
            foreach( $privilegeList as $key=>$val )
            {
                $privilegeIdArr[] = $val['id'];
            }
            $where          = [ 'privilege_id'=>['in',$privilegeIdArr], 'appnode_id'=>$appnodeId ];
            $appnodeList   = PrivilegeAppnode::getList( $where )->toArray();
            foreach( $appnodeList as $key=>$val )
            {
                $result[]   = $val['privilege_id'];
            }
        }
        return $result;
    }
    //取某个厂下具有某个功能的所有角色下的所有员工
    public static function getPrivilegeUser( $companyId, $appnodeId )
    {
        $privilegeIdArr         = self::getPrivilegeIds($companyId,$appnodeId);
        $result                 = [];
        if($privilegeIdArr)
        {
            $where              = ['company_id'=>$companyId,'privilege_id'=>['in',$privilegeIdArr]];
            $collection         = DepartmentUser::getList($where);
            $result             = $collection->toArray();
        }
        return $result;
    }

    public static function getWithDepartmentUser($privilegeIds){
        return self::whereIn( 'id', $privilegeIds)->with('departmentUser')->get();
    }

    public function departmentUser()
    {
        return $this->hasMany('App\Eloquent\Ygt\DepartmentUser','privilege_id');
    }

}